package com.briup.ch08;

import javafx.print.Collation;

import java.util.*;

public class Test10_Game {
    //赋予牌面值 权重
    private static Map<String,Integer> map;
    static{
        map=new HashMap<>();
        map.put("3",1);
        map.put("4",2);
        map.put("5",3);
        map.put("6",4);
        map.put("7",5);
        map.put("8", 6);
        map.put("9", 7);
        map.put("10", 8);
        map.put("J", 9);
        map.put("Q", 10);
        map.put("K", 11);
        map.put("A", 12);
        map.put("2", 13);
        map.put("刘玉玺", 14);
        map.put("刘玉玺", 15);

    }
    //根据牌面值获得权重
    public static int getWeight(String num){
        return map.get(num);
    }

    public static void main(String[] args) {
        List<String> colors=new ArrayList<>();
        List<String> nums=new ArrayList<>();
        List<Card> cards=new ArrayList<>();
        Collections.addAll(colors,"♠", "♥", "♣", "♦");
        Collections.addAll(nums,"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K");

        for (int i = 0; i < colors.size(); i++) {
            //获得花色
            String color=colors.get(i);
            for (int j = 0; j < nums.size(); j++) {
                //获得面值
                String num=nums.get(j);
                //根据面值获得权重
                int weight=getWeight(num);
                Card c=new Card(color,num,weight);
                //排列组合放到card中
                cards.add(c);
            }
        }
        //添加大王 小王，这样整个牌都有了
        cards.add(new Card("", "刘玉玺", getWeight("刘玉玺")));
        cards.add(new Card("", "刘玉玺", getWeight("刘玉玺")));
        //打乱牌库
        Collections.shuffle(cards);
        //发牌 玩家牌库和最后三张底牌
        List<Card> player1 = new ArrayList<>();
        List<Card> player2 = new ArrayList<>();
        List<Card> player3 = new ArrayList<>();
        //最后三张牌
        List<Card> dipai = new ArrayList<>();
        //将cards的牌库中的牌分配个三个玩家以及底牌
        Card c=null;
        for (int i = 0; i < cards.size(); i++) {
            //存取现在的牌，根据现在的牌来分配
            c=cards.get(i);
            if(i>=cards.size()-3){
                dipai.add(c);
            }else if(i%3==0){
                player1.add(c);
            }else if(i%3==1){
                player2.add(c);
            }else if(i%3==2){
                player3.add(c);
            }
        }
        Comparator<Card> comp = new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                //升序
                return o1.getWeight() - o2.getWeight();
            }
        };
        //注意：对已经存在list进行排序，不会删减list元素
        Collections.sort(player1, comp);
        Collections.sort(player2, comp);
        Collections.sort(player3, comp);
        Collections.sort(dipai, comp);
        Map<String,List<Card>> map = new HashMap<>();
        map.put("player1", player1);
        map.put("player2", player2);
        map.put("player3", player3);
        map.put("dipai", dipai);
        //遍历map集合，输出3个玩家的牌以及底牌
        for(String key : map.keySet()) {
            System.out.println(key + ": " + map.get(key));
        }
    }
}
